// JavaScript Document
/**
 * @description    : 瀑布流
 * @author         : laohu@staff.sina.com.cn
 * @create date    : 2012-03-15
 * @change date    :  
 * @change details : 
 * @parameter      : 
 * @details        : 
 * @return         :  */

(function(){
	
	if (!Array.indexOf) Array.prototype.indexOf = function(b) {
		for (var a = 0; a < this.length; a++) if (this[a] == b) return a;
		return - 1
	};
	
	function WaterFall(){}	
	WaterFall.prototype = {
		constructor : WaterFall,
		init : function( options ){
			var self = this;
			self.options = $.extend( {
				icontainerId : "waterfall",  //内容  ID
				loadingId : "iload",  //loading  ID
				pageId : "",    //分页ID
				url :  "/interface/pubu/json_get_pubu_data_by_type.php?callback=jsonp1333795092960&t=jsonp",       //获取数据地址
				scrollFixed : 100,
				pscreen : 8,
				start_value : 0,
				order : "time",
				show_del :0,
				show_add_album_pic:0,
				show_mblog_tag:0,
				isRandomComment : false,
				uid:0,
				show_type:"0",
				item_class:".pin",
				type : "common",
				col_num:5,
				pcount:"",
				start_page:1,
				callback : function(){}
			}, options || {} );
			
			self.icontainer = $("#" + self.options.icontainerId);
			self.iload = $("#" + self.options.loadingId);
			self.iload.before("<div id='___h' style='height:0;overflow:hidden;'></div>");
			self.iheight = $("#___h");
			//self.ipage = $("#" + self.options.pageId);
			self.loading = false;
			self.times = self.options.pscreen;
			self.count = parseInt(self.options.pscreen) + parseInt(self.options.start_page)+1;
		
			self.start_value =  self.options.start_value;
			self.firstLoad = 0;
			self.isOver = false;
			self.timer = null;
			self.$page = self.options.pageId == '' ? null : $("#" + self.options.pageId);
			
			self.icontainer.masonry({
			    itemSelector: self.options.item_class,
				columnWidth: function( containerWidth ) {
				return containerWidth / self.options.col_num ;
			  },
			  isAnimated:false,
			  isResizable:false
			 
			});

			
			self.initData()	
			self.bind_show_pic_info();	
			
			YOUXIXIU.MAIN.BindAddAlbum.bind_add_album();
			YOUXIXIU.MAIN.BindAddAlbum.bind_create_album();
			YOUXIXIU.MAIN.BindAddAlbum.bind_album_status_submit();
			YOUXIXIU.MAIN.BindAddAlbum.bind_page_slide();
			YOUXIXIU.MAIN.BindAddAlbum.bind_add_like();
				
			if(options.show_add_album_pic ==1)
			{
				YOUXIXIU.MAIN.BindAddAlbum.bind_add_album_pics();
				YOUXIXIU.MAIN.BindAddAlbum.bind_add_album_pics_submit();
				
			}
				

		},
		
		bind_show_pic_info:function()
		{
			
			var self = this;
			$(".pretty-pic img").die("mousemove");
			$(".pretty-pic img").live("mousemove",function(e){
				$(e.target).parents("li").find(".add-album").show();
				$(e.target).parents("li").find(".add-album-pics").show();
				//alert($(e.target).parents("li").find(".add-album-pics").parent().html())
				
			});
			
			$(".pretty-pic").find(".add-album").die("mousemove");
			$(".pretty-pic").find(".add-album").live("mousemove",function(e){
				
				clearTimeout(timer);
			});
			$(".pretty-pic").find(".add-album-pics").die("mousemove");
			$(".pretty-pic").find(".add-album-pics").live("mousemove",function(e){
				
				clearTimeout(timer);
			});

			$(".pretty-pic img").die("mouseout");
			$(".pretty-pic img").live("mouseout",function(e){
				self.setTimer();
			});
			
		},

		setTimer : function (){
			timer = setTimeout(function(){
				$(".add-album").hide();
				$(".add-album-pics").hide();
			}, 10);	
		},
		
		
		
		

		initData : function(){
			var self = this,
			options = self.options,
			sub_c = self.icontainer.find(".wfc");
			self.sub_c = sub_c;
			self.sub = [];
			self.dis = [];
			self.execute();
			if(self.$page){
				self.$page.hide();	
			}
			self=null;
		},
		
		_loadData : function(paramObj, url, callback){               //ajax加载数据

			var self = this;
			paramObj.t = "json";
			$.ajax({
				url : url,
				data : paramObj,
				dataType : 'json',
				success : function(data){
					if(parseInt(data.error, 10) === 0)
					{
						callback(data);
					}else{
						self._tip(data.errmsg);
					}					
				}
			});				
		},
		_tip : function(message){           //提示工具
			try{
				YOUXIXIU.MAIN.Tips.Show(message);

			}catch(e){
				alert(message);
			}				
		},	
		_renderData : function(data){
											
		},
		_handleData : function(){
			
			$(".loading").show();
			var self = this,
				options = self.options;
			self._loadData( {
				t : 'jsonp',
				start_value:self.start_value,
				pscreen : self.count - self.times,
				show_del : options.show_del,
				show_add_album_pic : options.show_add_album_pic,
				show_mblog_tag:options.show_mblog_tag,
				show_type:options.show_type,
				order:options.order,
				type:options.type,
				uid:options.uid,
				pcount:options.pcount
			}, options.url, function(data){
				$(".loading").hide();
			
				var $boxes = $(data.data);
				self.icontainer.append( $boxes ).masonry( "appended",$boxes );
				$boxes = null;
				if(data.is_finish){
					self.isOver = true;
					if(self.$page){
						self.$page.show();	
					}
					options.callback();
				}
				self.firstLoad++;
				
				self.loading = false;
			} );
			
			self.times--;	
			
		},
		execute : function(){			
			var self = this;
			if(self.timer){
				clearTimeout(self.timer);	
			}
			
			var	options = self.options,
				$window = $(window),
				$document = $(document),
				d = $window.scrollTop() + $window.height(),
				c = self.iheight.offset().top - options.scrollFixed;	

				
			if(  !self.loading && !self.isOver && (d > c ) ){
				self.iload.show();
				self.loading = true;
				setTimeout(function(){
					self._handleData();
				}, 100);				
			}
			
			!self.isOver && (self.timer = setTimeout(function(){
				
				self.execute();	
			}, 200));											
		}
	};
	

YOUXIXIU.MAIN.WATERFALL = function(o){
	var p = new WaterFall();
	p.init(o);
	return p;
};

 
})(jQuery);

